/**
 *240. 搜索二维矩阵 II
 * https://leetcode.cn/problems/search-a-2d-matrix-ii/description/
 */
class Solution2 {
    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length, n = matrix[0].length;
        //行二分
        for(int i = 0; i < m; i++) {
            int left = 0, right = n - 1;
            while(left <= right) {
                int mid = (left + right) / 2;
                if(matrix[i][mid] < target) {
                    left = mid + 1;
                } else if(matrix[i][mid] > target) {
                    right = mid - 1;
                } else return true;
            }
        }
        return false;
    }
}